package com.scy.java.map;

public class Person implements Comparable{
    private String name;
    private int age;

    public Person() {
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    /**
     * 重构compareTo()方法的实现逻辑
     * 需求：先按照年龄从小到大的顺序排列，（升序）
     * 如若年龄相等则按照名字从大到小的顺序排序（降序）
     * @param o the object to be compared.
     */
    @Override
    public int compareTo(Object o) {
        if (this == o) {
            //两个比较的对象相等返回0
            return 0;
        }
        if (o instanceof Person) {
            Person person = (Person) o;//向下造型
            //用当前对象年龄和传地过来的参数年龄进行比较
            int compare = this.age - person.age;
            if (compare != 0) {
                return compare;
            } else {
                //如若年龄相等则按照名字从大到小的顺序排序（降序）
                //     -降序          +升序
                //根据首字母排序
                return -this.name.compareTo(person.getName());
            }
        }else {
            throw new RuntimeException("传递的参数类型不匹配");
        }

    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }




}
